//
//  RecPhotoWallCell.m
//  CarFace
//
//  Created by dlios on 15/3/28.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "RecPhotoWallCell.h"

@interface RecPhotoWallCell () <UIScrollViewDelegate>

@property (nonatomic, retain) UIImageView *photoImgV;
@property (nonatomic, retain) UIScrollView *backScrollView;

@end

@implementation RecPhotoWallCell
- (void)dealloc
{
    [_photo release];
    [_photoImgV release];
    [_backScrollView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backScrollView = [[UIScrollView alloc] init];
//        self.backScrollView.backgroundColor = [UIColor whiteColor];
        self.backScrollView.delegate = self;
        self.backScrollView.maximumZoomScale = 2.0;
        self.backScrollView.minimumZoomScale = 1.0;
//        self.backScrollView.userInteractionEnabled = NO;
        self.backScrollView.showsHorizontalScrollIndicator = NO;
        self.backScrollView.showsVerticalScrollIndicator = NO;
        [self.contentView addSubview:self.backScrollView];
        [_backScrollView release];
        
        
        self.photoImgV = [[UIImageView alloc] init];
        _photoImgV.contentMode = UIViewContentModeScaleAspectFit;
//        _photoImgV.backgroundColor = [UIColor yellowColor];
        _photoImgV.clipsToBounds = YES;
        [self.backScrollView addSubview:_photoImgV];
        [_photoImgV release];
//        self.contentView.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    self.backScrollView.frame = layoutAttributes.bounds;
    self.photoImgV.frame = layoutAttributes.bounds;
}


- (void)setPhoto:(RecPhoto *)photo
{
    if (_photo != photo) {
        [_photo release];
        _photo = [photo retain];
    }

    NSString *str = [photo.imgurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    [_photoImgV setImageWithURL:url];
    
    NSLog(@"%@", url);
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{

    //当滑动结束的时候, 所有的图片回归原大小.
    self.backScrollView.zoomScale = 1;

}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return [self.backScrollView.subviews firstObject];
}

@end
